package com.duan.oauth.service.impl;

import com.duan.entity.Result;
import com.duan.oauth.mapper.UserMapper;
import com.duan.oauth.pojo.SysUser;
import com.duan.oauth.service.UserService;
import com.duan.user.feign.UserFeign;
import com.duan.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserFeign userFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        try {
            User userSelect = new User();
            userSelect.setUsername(username);
            User user = userFeign.select(userSelect).getData().get(0);
            SysUser sysUser = new SysUser();
            sysUser.setUsername(user.getUsername());
            sysUser.setPassword(user.getPassword());
            sysUser.setId(user.getId());
            sysUser.setStatus(user.getStatus());
            sysUser.setRoles(user.getRole());

            return sysUser;
        }catch (Exception e){
            return null;
        }

    }
}

